/*
 * Classname IBrokerManagerTest.java
 * 
 * Version Information
 * Date Feb 23, 2006
 *
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.isso.hibernate.tests;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import net.amsoft.iservice.isso.hibernate.IBroker;
import net.amsoft.iservice.isso.util.ISSOConstants;
import net.amsoft.iservice.isso.util.ISSOHelpers;
import net.amsoft.iservice.isso.util.Messages;
import net.amsoft.iservice.util.HibernateUtil;
import net.amsoft.iservice.util.exception.IServiceException;

/**
 * @author jyoti
 *  
 */
public class IBrokerManagerTest extends TestCase
{

    public static void main(String[] oArgs) {
        TestRunner.run(suite());
    } // main()

    public static Test suite() {
        return new TestSuite(IBrokerManagerTest.class);

    } // suite()

    protected void setUp() throws Exception {

        super.setUp();
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    private static void debug() {
        System.out.println("==================  DEBUG  ===================");
        System.out.println("####  IBROKER TABLE  ####");
        IBroker.listAll();

        System.out.println("==================  DEBUG  ===================");
    }

    private void setDB() throws SQLException {
        Connection oCon = null;
        try {
            oCon = getConnection();
            String strDeleteIBroker = "DELETE FROM IBROKER WHERE SERVER_NAME='"
                    + Messages.getString("ibroker.IBrokerName") + "'";
            PreparedStatement psDelete = oCon
                    .prepareStatement(strDeleteIBroker);
            //psDelete.execute();
            oCon.commit();
        } catch (Exception e) {
        } finally {
            oCon.close();
        }

    }

    public Connection getConnection() {
        Connection oCon = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String DB_URL = "jdbc:mysql://localhost:3306/hibernate";
            oCon = DriverManager.getConnection(DB_URL, "root", "root");
            oCon.setAutoCommit(false);
        } catch (Exception e) {
            System.out.println("Exception :" + e.getMessage());
        }
        return oCon;
    }
    private void setDBFromGet() throws SQLException {
        Connection oCon = null;
        try {
            oCon = getConnection();
            String strDeleteIBroker = "DELETE FROM IBROKER WHERE SERVER_NAME='"
                    + Messages.getString("ibroker.IBrokerName") + "'";
            PreparedStatement psDelete = oCon
                    .prepareStatement(strDeleteIBroker);
            psDelete.execute();
            oCon.commit();
        } catch (Exception e) {
        }

        try {
            oCon = getConnection();
            String strCreateIBroker = "INSERT INTO IBROKER (SERVER_NAME , STATE_ID )VALUES ('"
                    + Messages.getString("ibroker.IBrokerName") + "' , 1";
            PreparedStatement psCreate = oCon
                    .prepareStatement(strCreateIBroker);
            psCreate.execute();
            oCon.commit();
        } catch (Exception e) {
        }

        finally {
            oCon.close();
        }

    }

    private ResultSet getDataFromDB(String sql, Connection oCon) {
        String strSQLGet = sql;

        try {
            PreparedStatement psGet = oCon.prepareStatement(strSQLGet);
            ResultSet oRs = psGet.executeQuery();
            return oRs;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;

    }

    public void testCreate() throws IServiceException, SQLException {

        try {
            IBroker oIBroker = new IBroker();
            oIBroker.setServerName(Messages.getString("ibroker.IBrokerName"));//("isso.ibroker10.amsoft.co.in");
            oIBroker.setDescription("broker ");

            oIBroker.setState((short) 1);
            oIBroker.insert();
            debug();

        } catch (Exception e) {
            e.printStackTrace();

        }

        Connection oCon = getConnection();

        String sqlGet = "SELECT SERVER_NAME FROM IBROKER WHERE "
                + "SERVER_NAME = '" + Messages.getString("ibroker.IBrokerName")
                + "'";

        ResultSet rs = getDataFromDB(sqlGet, oCon);

        rs.next();
        assertEquals(rs.getString(1), Messages.getString("ibroker.IBrokerName"));
        oCon.close();

    }

    public void testUpdateMultiplefields() throws IServiceException,
            SQLException {

        System.out.println("-----------IBROKER UPDATE"
                + Messages.getString("ibroker.IBrokerName"));
        IBroker oIBroker = ISSOHelpers.fetchIBroker(Messages
                .getString("ibroker.IBrokerName"));

        //    				
        System.out.println("-----------IBROKER UPDATE"
                + Messages.getString("ibroker.IBrokerName"));
        oIBroker.setDescription(Messages
                .getString("ibroker.HomepageURL.update"));
        System.out.println("-----------IBROKER UPDATE"
                + Messages.getString("ibroker.HomepageURL.update"));
        oIBroker.update();
        debug();

        Connection oCon = null;
        try {
            oCon = getConnection();
            String sqlGet = "SELECT DESCRIPTION FROM IBROKER WHERE "
                    + "SERVER_NAME = '"
                    + Messages.getString("ibroker.IBrokerName") + "'";

            ResultSet rs = getDataFromDB(sqlGet, oCon);
            rs.next();
            assertEquals(rs.getString(1), Messages
                    .getString("ibroker.HomepageURL.update"));

        } catch (Exception e) {
            fail("Update does not work correctly");
        } finally {
            oCon.close();
        }
    }

    public void testGet() throws IServiceException, SQLException {
        IBroker oIBroker = ISSOHelpers.fetchIBroker(Messages
                .getString("ibroker.IBrokerName"));

        Connection oCon = null;
        try {
            oCon = getConnection();
            String sqlGet = "SELECT IBROKER_ID , SERVER_NAME "
                    + " FROM IBROKER WHERE " + "SERVER_NAME = '"
                    + Messages.getString("ibroker.IBrokerName") + "'";

            ResultSet rs = getDataFromDB(sqlGet, oCon);
            rs.next();

            assertEquals(rs.getString(2), oIBroker.getServerName());

        } catch (Exception e) {
            fail("Update does not work correctly");
        } finally {
            oCon.close();
        }
    }

    public void testDelete() throws SQLException {

        Connection oCon = null;

        try {
            IBroker oIBroker = ISSOHelpers.fetchIBroker(Messages
                    .getString("ibroker.IBrokerName"));
            oIBroker.remove();
            debug();
            oCon = getConnection();
            String sqlGet = "SELECT STATE_ID FROM IBROKER WHERE "
                    + "SERVER_NAME = '"
                    + Messages.getString("ibroker.IBrokerName") + "'";

            ResultSet rs = getDataFromDB(sqlGet, oCon);
            rs.next();
            assertEquals(rs.getInt(1), (short) ISSOConstants.STATE_INACTIVE);

        } catch (Exception e) {
            fail("Delete does not work correctly");
        } finally {
            oCon.close();
            HibernateUtil.getSessionFactory().close();
        }

    }

}